package com.example.internet;


import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

public class MyBaseAdapter extends BaseAdapter {
    private List<Computet> list;
    private Context mContext;

    public MyBaseAdapter(Context mcontext,List<Computet> list){
        this.mContext = mcontext;
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.computer_info,null);
            //对viewHolder的属性进行赋值
            viewHolder.tvDate = convertView.findViewById(R.id.tv_date);

            viewHolder.tvNum= convertView.findViewById(R.id.tv_num);

            viewHolder.tvState1 = convertView.findViewById(R.id.tv_state1);

            viewHolder.tvState2 = convertView.findViewById(R.id.tv_state2);

            viewHolder.linearLayout =convertView.findViewById(R.id.line1);

            //使用setTag缓存起来方便多次重用
            convertView.setTag(viewHolder);
        }
        else{ //如果缓存池中有对应的缓存，则直接通过getTag取出viewHolder
            viewHolder = (ViewHolder) convertView.getTag();
        }


        final Computet info = list.get(position);

        viewHolder.tvDate.setText(info.getDate());

        viewHolder.tvNum.setText("编号:"+info.getNum());
        viewHolder.tvState1.setText("使用:"+info.getState1());
        viewHolder.tvState2.setText("维修:"+info.getState2());
        //更换字体颜色
        if (info.getState1().equals("是")){
            viewHolder.tvState1.setTextColor(mContext.getResources().getColor(R.color.state1));
        }
        if (info.getState2().equals("是")){
            viewHolder.tvState2.setTextColor(mContext.getResources().getColor(R.color.state2));
        }


        //设置点击事件  修改信息
        viewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("type", "修改");
                intent.putExtra("data", info.getDate());
                intent.putExtra("num", info.getNum());
                intent.putExtra("state1", info.getState1());
                intent.putExtra("state2", info.getState2());
                intent.setClass(mContext,ActivityAdd.class);
                mContext.startActivity(intent);
            }
        });

        return convertView;
    }

    class ViewHolder{                //用于缓存控件
        TextView tvDate;
        TextView tvNum;
        TextView tvState1;
        TextView tvState2;
        LinearLayout linearLayout;
    }
}